package com.mondo.financialwallet.presentation.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.widget.EditText;

import com.example.common.ui.BaseActivity;
import com.mondo.financialwallet.R;
import com.mondo.financialwallet.data.DataRepository;
import com.mondo.financialwallet.presentation.event.AlterNicknameEvent;
import com.mondo.financialwallet.presentation.utils.ActivityTitleBarHelper;
import com.mondo.financialwallet.presentation.utils.Toasts;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;

/**
 * 修改昵称
 */
public class AlterNicknameActivity extends BaseActivity {

    private static final String EXTRA_NICKNAME = "NICKNAME";

    public static void launch(Context context, String nickname) {
        context.startActivity(new Intent(context, AlterNicknameActivity.class)
                .putExtra(EXTRA_NICKNAME, nickname));
    }

    @Bind(R.id.new_nickname_edit) EditText mNewNicknameEdit;

    @Override
    protected void setStatusBar() {
        ActivityTitleBarHelper.setWhiteDarkColorStatus(this);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_alter_nickname;
    }

    @Override
    protected void initActions() {
        ActivityTitleBarHelper.init(this, R.string.title_alter_nickname, R.string.text_save, v -> alterNickname());
        mNewNicknameEdit.setHint(getIntent().getStringExtra(EXTRA_NICKNAME));
    }

    private void alterNickname() {
        String nickname = mNewNicknameEdit.getText().toString();
        DataRepository.get().alterNickname(nickname, result -> {
            if (result.isSuccess()) {
                Toasts.success(R.string.message_alter_success);
                EventBus.getDefault().post(new AlterNicknameEvent(nickname));
                finish();
            } else {
                Toasts.failure(result.getMsg());
            }
        });
    }
}
